﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.FeatureBase;
using QLBH.ModelInterface;
using QLBH.Model.Controller;
using QLBH.DataModel;
using QLBH.ModelFactory;
using System.Windows.Forms;

namespace QLBH.CongNo
{
    public class CongNoView : FormViewBase
    {
        public CongNoView()
            :base()
        {
            InitializeComponent();

            datasource = new List<ISoNoModel>();
            
        }        
#region properties
        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.ToolStripButton btnGhiNo;
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
        private StandardUI.DataGridViewEx dataGridViewEx1;
        private System.Windows.Forms.ToolStripButton btnThuNo;
        private System.Windows.Forms.ToolStripButton btnTraNo;
        private System.Windows.Forms.ToolStripButton btnLichSuTraNo;
        private System.Windows.Forms.ToolStripButton btnLichSuThuNo;
        private System.Windows.Forms.Panel panel2;
            
#endregion
        private RadioButton rbtnKHNo;
        private RadioButton rbtnNoNCC;
        private RadioButton rbtnNCCNO;
        private RadioButton rbtnNoKH;
        private DataGridViewTextBoxColumn sonoid;
        private DataGridViewTextBoxColumn khid;
        private DataGridViewTextBoxColumn nccid;
        private DataGridViewTextBoxColumn tenkh;
        private DataGridViewTextBoxColumn tenncc;
        private DataGridViewTextBoxColumn sotien;
        private DataGridViewTextBoxColumn datra;
        private DataGridViewTextBoxColumn lydoid;
        private DataGridViewTextBoxColumn tenlydo;
        private DataGridViewTextBoxColumn ngaytao;
        private DataGridViewTextBoxColumn ngaycapnhat;
        private DataGridViewTextBoxColumn nguoitaoid;
        private DataGridViewTextBoxColumn NGUOITAO;
        private DataGridViewCheckBoxColumn LOAI;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripButton btnFind;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripButton btnRefresh;

        private List<ISoNoModel> datasource;
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.dataGridViewEx1 = new QLBH.StandardUI.DataGridViewEx();
            this.sonoid = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.khid = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.nccid = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.tenkh = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.tenncc = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.sotien = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.datra = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.lydoid = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.tenlydo = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.ngaytao = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.ngaycapnhat = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.nguoitaoid = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.NGUOITAO = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.LOAI = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.panel2 = new System.Windows.Forms.Panel();
            this.rbtnNoNCC = new System.Windows.Forms.RadioButton();
            this.rbtnNCCNO = new System.Windows.Forms.RadioButton();
            this.rbtnNoKH = new System.Windows.Forms.RadioButton();
            this.rbtnKHNo = new System.Windows.Forms.RadioButton();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.btnGhiNo = new System.Windows.Forms.ToolStripButton();
            this.btnThuNo = new System.Windows.Forms.ToolStripButton();
            this.btnTraNo = new System.Windows.Forms.ToolStripButton();
            this.btnLichSuTraNo = new System.Windows.Forms.ToolStripButton();
            this.btnLichSuThuNo = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnFind = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.btnRefresh = new System.Windows.Forms.ToolStripButton();
            this.panel1.SuspendLayout();
            this.tableLayoutPanel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridViewEx1)).BeginInit();
            this.panel2.SuspendLayout();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.tableLayoutPanel1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 25);
            this.panel1.Margin = new System.Windows.Forms.Padding(10);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(0, 0);
            this.panel1.TabIndex = 4;
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 1;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
            this.tableLayoutPanel1.Controls.Add(this.dataGridViewEx1, 0, 1);
            this.tableLayoutPanel1.Controls.Add(this.panel2, 0, 0);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 2;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 36F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(0, 0);
            this.tableLayoutPanel1.TabIndex = 0;
            // 
            // dataGridViewEx1
            // 
            this.dataGridViewEx1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
            this.dataGridViewEx1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this.dataGridViewEx1.BackgroundColor = System.Drawing.Color.Gray;
            this.dataGridViewEx1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridViewEx1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.sonoid,
            this.khid,
            this.nccid,
            this.tenkh,
            this.tenncc,
            this.sotien,
            this.datra,
            this.lydoid,
            this.tenlydo,
            this.ngaytao,
            this.ngaycapnhat,
            this.nguoitaoid,
            this.NGUOITAO,
            this.LOAI});
            this.dataGridViewEx1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridViewEx1.Location = new System.Drawing.Point(3, 39);
            this.dataGridViewEx1.MultiSelect = false;
            this.dataGridViewEx1.Name = "dataGridViewEx1";
            this.dataGridViewEx1.ReadOnly = true;
            this.dataGridViewEx1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridViewEx1.Size = new System.Drawing.Size(44, 1);
            this.dataGridViewEx1.TabIndex = 0;
            this.dataGridViewEx1.DataSourceChanged += new System.EventHandler(this.dataGridViewEx1_DataSourceChanged);
            // 
            // sonoid
            // 
            this.sonoid.DataPropertyName = "SONOID";
            this.sonoid.HeaderText = "soniid";
            this.sonoid.Name = "sonoid";
            this.sonoid.ReadOnly = true;
            this.sonoid.Visible = false;
            this.sonoid.Width = 59;
            // 
            // khid
            // 
            this.khid.DataPropertyName = "KHID";
            this.khid.HeaderText = "khid";
            this.khid.Name = "khid";
            this.khid.ReadOnly = true;
            this.khid.Visible = false;
            this.khid.Width = 52;
            // 
            // nccid
            // 
            this.nccid.DataPropertyName = "NCCID";
            this.nccid.HeaderText = "nccid";
            this.nccid.Name = "nccid";
            this.nccid.ReadOnly = true;
            this.nccid.Visible = false;
            this.nccid.Width = 58;
            // 
            // tenkh
            // 
            this.tenkh.DataPropertyName = "TENKH";
            this.tenkh.HeaderText = "Tên KH";
            this.tenkh.Name = "tenkh";
            this.tenkh.ReadOnly = true;
            this.tenkh.Width = 78;
            // 
            // tenncc
            // 
            this.tenncc.DataPropertyName = "TENNCC";
            this.tenncc.HeaderText = "Tên NCC";
            this.tenncc.Name = "tenncc";
            this.tenncc.ReadOnly = true;
            this.tenncc.Width = 88;
            // 
            // sotien
            // 
            this.sotien.DataPropertyName = "SOTIEN";
            this.sotien.HeaderText = "Số tiền(VNĐ)";
            this.sotien.Name = "sotien";
            this.sotien.ReadOnly = true;
            this.sotien.Width = 110;
            // 
            // datra
            // 
            this.datra.DataPropertyName = "DATRA";
            this.datra.HeaderText = "Đã trả(VNĐ)";
            this.datra.Name = "datra";
            this.datra.ReadOnly = true;
            this.datra.Width = 104;
            // 
            // lydoid
            // 
            this.lydoid.DataPropertyName = "LYDOID";
            this.lydoid.HeaderText = "lydoid";
            this.lydoid.Name = "lydoid";
            this.lydoid.ReadOnly = true;
            this.lydoid.Visible = false;
            this.lydoid.Width = 59;
            // 
            // tenlydo
            // 
            this.tenlydo.DataPropertyName = "TENLYDO";
            this.tenlydo.HeaderText = "Lý do";
            this.tenlydo.Name = "tenlydo";
            this.tenlydo.ReadOnly = true;
            this.tenlydo.Width = 66;
            // 
            // ngaytao
            // 
            this.ngaytao.DataPropertyName = "NGAYTAO";
            this.ngaytao.HeaderText = "Ngày tạo";
            this.ngaytao.Name = "ngaytao";
            this.ngaytao.ReadOnly = true;
            this.ngaytao.Width = 88;
            // 
            // ngaycapnhat
            // 
            this.ngaycapnhat.DataPropertyName = "NGAYCAPNHAT";
            this.ngaycapnhat.HeaderText = "Ngày cập nhật";
            this.ngaycapnhat.Name = "ngaycapnhat";
            this.ngaycapnhat.ReadOnly = true;
            this.ngaycapnhat.Visible = false;
            this.ngaycapnhat.Width = 102;
            // 
            // nguoitaoid
            // 
            this.nguoitaoid.DataPropertyName = "USERID";
            this.nguoitaoid.HeaderText = "NGUOITAOID";
            this.nguoitaoid.Name = "nguoitaoid";
            this.nguoitaoid.ReadOnly = true;
            this.nguoitaoid.Visible = false;
            // 
            // NGUOITAO
            // 
            this.NGUOITAO.DataPropertyName = "USERNAME";
            this.NGUOITAO.HeaderText = "Người tạo";
            this.NGUOITAO.Name = "NGUOITAO";
            this.NGUOITAO.ReadOnly = true;
            this.NGUOITAO.Width = 91;
            // 
            // LOAI
            // 
            this.LOAI.DataPropertyName = "LOAINO";
            this.LOAI.HeaderText = "LOAI";
            this.LOAI.Name = "LOAI";
            this.LOAI.ReadOnly = true;
            this.LOAI.Visible = false;
            this.LOAI.Width = 37;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.rbtnNoNCC);
            this.panel2.Controls.Add(this.rbtnNCCNO);
            this.panel2.Controls.Add(this.rbtnNoKH);
            this.panel2.Controls.Add(this.rbtnKHNo);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel2.Location = new System.Drawing.Point(3, 3);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(44, 30);
            this.panel2.TabIndex = 1;
            // 
            // rbtnNoNCC
            // 
            this.rbtnNoNCC.AutoSize = true;
            this.rbtnNoNCC.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.rbtnNoNCC.Location = new System.Drawing.Point(339, 3);
            this.rbtnNoNCC.Name = "rbtnNoNCC";
            this.rbtnNoNCC.Size = new System.Drawing.Size(75, 20);
            this.rbtnNoNCC.TabIndex = 0;
            this.rbtnNoNCC.Text = "Nợ NCC";
            this.rbtnNoNCC.UseVisualStyleBackColor = true;
            this.rbtnNoNCC.CheckedChanged += new System.EventHandler(this.LoaiNo_CheckedChanged);
            // 
            // rbtnNCCNO
            // 
            this.rbtnNCCNO.AutoSize = true;
            this.rbtnNCCNO.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.rbtnNCCNO.Location = new System.Drawing.Point(239, 3);
            this.rbtnNCCNO.Name = "rbtnNCCNO";
            this.rbtnNCCNO.Size = new System.Drawing.Size(75, 20);
            this.rbtnNCCNO.TabIndex = 0;
            this.rbtnNCCNO.Text = "NCC Nợ";
            this.rbtnNCCNO.UseVisualStyleBackColor = true;
            this.rbtnNCCNO.CheckedChanged += new System.EventHandler(this.LoaiNo_CheckedChanged);
            // 
            // rbtnNoKH
            // 
            this.rbtnNoKH.AutoSize = true;
            this.rbtnNoKH.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.rbtnNoKH.Location = new System.Drawing.Point(119, 3);
            this.rbtnNoKH.Name = "rbtnNoKH";
            this.rbtnNoKH.Size = new System.Drawing.Size(116, 20);
            this.rbtnNoKH.TabIndex = 0;
            this.rbtnNoKH.Text = "Nợ khách hàng";
            this.rbtnNoKH.UseVisualStyleBackColor = true;
            this.rbtnNoKH.CheckedChanged += new System.EventHandler(this.LoaiNo_CheckedChanged);
            // 
            // rbtnKHNo
            // 
            this.rbtnKHNo.AutoSize = true;
            this.rbtnKHNo.Checked = true;
            this.rbtnKHNo.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.rbtnKHNo.Location = new System.Drawing.Point(3, 3);
            this.rbtnKHNo.Name = "rbtnKHNo";
            this.rbtnKHNo.Size = new System.Drawing.Size(114, 20);
            this.rbtnKHNo.TabIndex = 0;
            this.rbtnKHNo.TabStop = true;
            this.rbtnKHNo.Text = "Khách hàng nợ";
            this.rbtnKHNo.UseVisualStyleBackColor = true;
            this.rbtnKHNo.CheckedChanged += new System.EventHandler(this.LoaiNo_CheckedChanged);
            // 
            // toolStrip1
            // 
            this.toolStrip1.BackColor = System.Drawing.Color.Transparent;
            this.toolStrip1.BackgroundImage = global::QLBH.CongNo.Properties.Resources.ToolbarBackgroud;
            this.toolStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnGhiNo,
            this.btnThuNo,
            this.btnTraNo,
            this.btnLichSuTraNo,
            this.btnLichSuThuNo,
            this.toolStripSeparator1,
            this.btnFind,
            this.toolStripSeparator2,
            this.btnRefresh});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(0, 25);
            this.toolStrip1.TabIndex = 3;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // btnGhiNo
            // 
            this.btnGhiNo.Image = global::QLBH.CongNo.Properties.Resources.GhiNo;
            this.btnGhiNo.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnGhiNo.Name = "btnGhiNo";
            this.btnGhiNo.Size = new System.Drawing.Size(64, 22);
            this.btnGhiNo.Text = "Ghi Nợ";
            this.btnGhiNo.Click += new System.EventHandler(this.btnGhiNo_Click);
            // 
            // btnThuNo
            // 
            this.btnThuNo.Image = global::QLBH.CongNo.Properties.Resources.GhiNo;
            this.btnThuNo.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnThuNo.Name = "btnThuNo";
            this.btnThuNo.Size = new System.Drawing.Size(67, 22);
            this.btnThuNo.Text = "Thu Nợ";
            this.btnThuNo.Click += new System.EventHandler(this.btnThuNo_Click);
            // 
            // btnTraNo
            // 
            this.btnTraNo.Image = global::QLBH.CongNo.Properties.Resources.GhiNo;
            this.btnTraNo.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnTraNo.Name = "btnTraNo";
            this.btnTraNo.Size = new System.Drawing.Size(61, 22);
            this.btnTraNo.Text = "Trả nợ";
            this.btnTraNo.Click += new System.EventHandler(this.btnTraNo_Click);
            // 
            // btnLichSuTraNo
            // 
            this.btnLichSuTraNo.Image = global::QLBH.CongNo.Properties.Resources.GhiNo;
            this.btnLichSuTraNo.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnLichSuTraNo.Name = "btnLichSuTraNo";
            this.btnLichSuTraNo.Size = new System.Drawing.Size(98, 22);
            this.btnLichSuTraNo.Text = "Lịch sử trả nợ";
            this.btnLichSuTraNo.Click += new System.EventHandler(this.btnLichSuTraNo_Click);
            // 
            // btnLichSuThuNo
            // 
            this.btnLichSuThuNo.Image = global::QLBH.CongNo.Properties.Resources.GhiNo;
            this.btnLichSuThuNo.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnLichSuThuNo.Name = "btnLichSuThuNo";
            this.btnLichSuThuNo.Size = new System.Drawing.Size(102, 22);
            this.btnLichSuThuNo.Text = "Lịch sử thu nợ";
            this.btnLichSuThuNo.Click += new System.EventHandler(this.btnLichSuThuNo_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // btnFind
            // 
            this.btnFind.Image = global::QLBH.CongNo.Properties.Resources.search_icon16;
            this.btnFind.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnFind.Name = "btnFind";
            this.btnFind.Size = new System.Drawing.Size(118, 22);
            this.btnFind.Text = "Tìm kiếm (Ctr+F)";
            this.btnFind.Click += new System.EventHandler(this.btnFind_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // btnRefresh
            // 
            this.btnRefresh.Image = global::QLBH.CongNo.Properties.Resources.Refresh_icon16;
            this.btnRefresh.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnRefresh.Name = "btnRefresh";
            this.btnRefresh.Size = new System.Drawing.Size(87, 22);
            this.btnRefresh.Text = "Nạp lại (F5)";
            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            // 
            // CongNoView
            // 
            this.BackColor = System.Drawing.SystemColors.ControlDark;
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.toolStrip1);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Margin = new System.Windows.Forms.Padding(0);
            this.Name = "CongNoView";
            this.Size = new System.Drawing.Size(0, 0);
            this.Load += new System.EventHandler(this.CongNoView_Load);
            this.panel1.ResumeLayout(false);
            this.tableLayoutPanel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridViewEx1)).EndInit();
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void CongNoView_Load(object sender, EventArgs e)
        {
            btnThuNo.Enabled = true;
            btnTraNo.Enabled = false;
            btnLichSuThuNo.Enabled = true;
            btnLichSuTraNo.Enabled = false;

            LoadDSNo();
        }
        private void LoadDSNo()
        {
            this.Cursor = Cursors.WaitCursor;
            datasource.Clear();

            List<SONO> sonos=SoNoController.GetInstance().GetAll().ToList();

            foreach (SONO sono in sonos)
            {
                ISoNoModel sonomodel = CongNoModelFactory.GetSoNoModelInstance();
                sonomodel.InitFromEntity(sono);

                datasource.Add(sonomodel);
            }

            if (rbtnNoKH.Checked || rbtnNoNCC.Checked)
            {
                if (rbtnNoKH.Checked)
                {
                    //No KH

                    datasource = datasource.Where(sn => sn.LOAINO == false && sn.NCCID==Guid.Empty).ToList();

                    dataGridViewEx1.Columns["tenkh"].Visible = true;
                    dataGridViewEx1.Columns["tenncc"].Visible = false;                    
                }
                else
                {
                    //No NCC
                    datasource = datasource.Where(sn => sn.LOAINO == false && sn.KHID == Guid.Empty).ToList();

                    dataGridViewEx1.Columns["tenkh"].Visible = false;
                    dataGridViewEx1.Columns["tenncc"].Visible = true;                    
                }
            }
            else if(rbtnKHNo.Checked || rbtnNCCNO.Checked)
            {                
                if (rbtnKHNo.Checked)
                {
                    //KH No

                    datasource = datasource.Where(sn => sn.LOAINO == true && sn.NCCID==Guid.Empty).ToList();

                    dataGridViewEx1.Columns["tenkh"].Visible = true;
                    dataGridViewEx1.Columns["tenncc"].Visible = false;
                }
                else
                {
                   // NCC no

                    datasource = datasource.Where(sn => sn.LOAINO == true && sn.KHID == Guid.Empty).ToList();

                    dataGridViewEx1.Columns["tenkh"].Visible = false;
                    dataGridViewEx1.Columns["tenncc"].Visible = true;
                }
            }            
            dataGridViewEx1.DataSource = datasource;

            this.Cursor = Cursors.Default;
        }

        private void btnGhiNo_Click(object sender, EventArgs e)
        {
            GhiNoDlg ghinoDlg = new GhiNoDlg();
            if (rbtnKHNo.Checked)
            {
                ghinoDlg.LoaiNo = eLoaiNo.LN_KHNO;
            }
            else if (rbtnNCCNO.Checked)
            {
                ghinoDlg.LoaiNo = eLoaiNo.LN_NCCNO;
            }
            else if (rbtnNoKH.Checked)
            {
                ghinoDlg.LoaiNo = eLoaiNo.LN_NOKH;
            }
            else
            {
                ghinoDlg.LoaiNo = eLoaiNo.LN_NONCC;
            }
            if (ghinoDlg.ShowDialog()==DialogResult.OK)
            {
                LoadDSNo();
            }
        }

        private void btnThuNo_Click(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count>0)
            {
                ISoNoModel selectedItem = dataGridViewEx1.SelectedRows[0].DataBoundItem as ISoNoModel;
                Thu_Tra_NoDlg thunodlg = new Thu_Tra_NoDlg(eThuTraNO.THUNO);
                thunodlg.Model = selectedItem;
                if (thunodlg.ShowDialog() == DialogResult.OK)
                {
                    LoadDSNo();
                }
            }
            
        }

        private void btnTraNo_Click(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count>0)
            {
                ISoNoModel selectedItem = dataGridViewEx1.SelectedRows[0].DataBoundItem as ISoNoModel;
                Thu_Tra_NoDlg thunodlg = new Thu_Tra_NoDlg(eThuTraNO.TRANO);
                thunodlg.Model = selectedItem;
                if (thunodlg.ShowDialog() == DialogResult.OK)
                {
                    LoadDSNo();
                }
            }            
        }

        private void LoaiNo_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                LoadDSNo();
                if (dataGridViewEx1.RowCount>0)
                {
                    if (rbtnKHNo.Checked || rbtnNCCNO.Checked)
                    {
                        btnThuNo.Enabled = true;
                        btnTraNo.Enabled = false;
                        btnLichSuThuNo.Enabled = true;
                        btnLichSuTraNo.Enabled = false;
                    }
                    else
                    {
                        btnThuNo.Enabled = false;
                        btnTraNo.Enabled = true;
                        btnLichSuThuNo.Enabled = false;
                        btnLichSuTraNo.Enabled = true;
                    }
                }
                
            }
        }

        private void dataGridViewEx1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridViewEx1.RowCount>0)
            {
                if (rbtnKHNo.Checked || rbtnNCCNO.Checked)
                {
                    btnThuNo.Enabled = true;
                    btnTraNo.Enabled = false;
                    btnLichSuThuNo.Enabled = true;
                    btnLichSuTraNo.Enabled = false;
                }
                else
                {
                    btnThuNo.Enabled = false;
                    btnTraNo.Enabled = true;
                    btnLichSuThuNo.Enabled = false;
                    btnLichSuTraNo.Enabled = true;
                }
            }
            else
            {
                btnLichSuThuNo.Enabled = false;
                btnLichSuTraNo.Enabled = false;
                btnThuNo.Enabled = false;
                btnTraNo.Enabled = false;
            }
        }
        public override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Control && e.KeyCode==Keys.F)
            {
                btnFind_Click(btnFind, null);
            }
            else if (e.KeyCode==Keys.F5)
            {
                btnRefresh_Click(btnRefresh, null);
            }
            base.OnKeyDown(e);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            frmFindDlg searchDlg=new frmFindDlg();            
            if (searchDlg.ShowDialog()==DialogResult.OK)
            {
                String searchInput = searchDlg.SearchInput;
                if (rbtnKHNo.Checked || rbtnNoKH.Checked)                
                {
                    List<ISoNoModel> findResult = datasource.Where(et => et.TENKH.Contains(searchInput)).ToList();
                    dataGridViewEx1.DataSource = findResult;
                }
                else
                {
                    List<ISoNoModel> findResult = datasource.Where(et => et.TENNCC.Contains(searchInput)).ToList();
                    dataGridViewEx1.DataSource = findResult;
                }                
            }             
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadDSNo();
        }

        private void btnLichSuThuNo_Click(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count>0)
            {
                ISoNoModel selectedItem = dataGridViewEx1.SelectedRows[0].DataBoundItem as ISoNoModel;
                eLoaiNo loaino;
                if (rbtnKHNo.Checked)
                {
                    loaino=eLoaiNo.LN_KHNO;
                }
                else if (rbtnNCCNO.Checked)
                {
                    loaino=eLoaiNo.LN_NCCNO;
                }
                else if (rbtnNoKH.Checked)
                {
                    loaino=eLoaiNo.LN_NONCC;
                }
                else
                {
                    loaino=eLoaiNo.LN_NONCC;
                }

                frmThuTraNoHistoryDlg historyDlg = new frmThuTraNoHistoryDlg(loaino);
                historyDlg.SonoModel = selectedItem;

                historyDlg.ShowDialog();
            }
        }

        private void btnLichSuTraNo_Click(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count > 0)
            {
                ISoNoModel selectedItem = dataGridViewEx1.SelectedRows[0].DataBoundItem as ISoNoModel;
                eLoaiNo loaino;
                if (rbtnKHNo.Checked)
                {
                    loaino = eLoaiNo.LN_KHNO;
                }
                else if (rbtnNCCNO.Checked)
                {
                    loaino = eLoaiNo.LN_NCCNO;
                }
                else if (rbtnNoKH.Checked)
                {
                    loaino = eLoaiNo.LN_NOKH;
                }
                else
                {
                    loaino = eLoaiNo.LN_NONCC;
                }

                frmThuTraNoHistoryDlg historyDlg = new frmThuTraNoHistoryDlg(loaino);
                historyDlg.SonoModel = selectedItem;

                historyDlg.ShowDialog();
            }
        }
    }
}
